<?php
namespace app\admin\controller;

use OSS\Core\OssException;
use OSS\OssClient;
use think\App;

/**
 * 阿里云文件管理
 * Class File
 * @package app\admin\controller
 */
class File extends Base {
    // 存储空间名称
    private $bucket ="medical-course";

    // Endpoint以杭州为例，其它Region请按实际情况填写。
    //$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    private $endpoint = "http://oss-cn-shenzhen.aliyuncs.com";

    // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
    private $accessKeyId = "";
    private $accessKeySecret = "";

    private $ossClient;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->accessKeyId = env('aliyun.ali_access_key_id');
        $this->accessKeySecret = env('aliyun.ali_access_key_secret');
        $this->ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
    }


    public function upload(){
        try{
            // <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径，例如abc/efg/123.jpg
            // 设置文件名称。
            $object = "uploads/testimage.jpg";
            // <yourLocalFile>由本地文件路径加文件名包括后缀组成，例如/users/local/myfile.txt。
            $filePath = "/www/wwwroot/yike/public/1.jpg";

            $this->ossClient->uploadFile($this->bucket, $object, $filePath);
        } catch(OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
    }

    /**
     * 生成阿里云上的文件名称
     */
    private function createFileName(){

    }

    public function fileList(){
        $nextMarker = '';
        while (true) {
            try {
                $options = array(
                    'delimiter' => '',
                    'marker' => $nextMarker,
                );
                $listObjectInfo = $this->ossClient->listObjects($this->bucket, $options);
            } catch (OssException $e) {
                printf(__FUNCTION__ . ": FAILED\n");
                printf($e->getMessage() . "\n");
                return;
            }
            // 得到nextMarker，从上一次listObjects读到的最后一个文件的下一个文件开始继续获取文件列表。
            $nextMarker = $listObjectInfo->getNextMarker();
            $listObject = $listObjectInfo->getObjectList();
            $listPrefix = $listObjectInfo->getPrefixList();

            if (!empty($listObject)) {
                print("objectList:\n");
                foreach ($listObject as $objectInfo) {
                    print($objectInfo->getKey() . "\n");
                }
            }
            if (!empty($listPrefix)) {
                print("prefixList: \n");
                foreach ($listPrefix as $prefixInfo) {
                    print($prefixInfo->getPrefix() . "\n");
                }
            }
            if ($listObjectInfo->getIsTruncated() !== "true") {
                break;
            }
        }
    }
}